﻿using HuiZe.education.Common;
using HuiZe.education.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HuiZe.education.ViewModel
{
    public class ShareResourceViewModel : ViewModelBase
    {
        public ShareResourceViewModel()
        {

        }

        public int pageCount { set; get; }

        public List<resShareList> GetShareList(int Type, int pages, string _key = "")
        {
            reqShareList reqClassInfoModel = new reqShareList()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                limit = 12,
                page = pages,
                type = Type,
                search = _key,
            };
            string data = JsonHelper.ToJson(reqClassInfoModel);
            var result = Request<resShareListMain>("/api/resources/getShareList", data, "共享资源包列表");
            if (result.Item1)
            {
                pageCount = result.Item2.data.last_page;
                return result.Item2.data.data;
            }
            else
            {
                return null;
            }
        }

        public List<resShareList> GetMyShareList(int Type)
        {
            reqShareList reqClassInfoModel = new reqShareList()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                limit = 15,
                page = 1,
                type = Type,
                user = 1
            };
            string data = JsonHelper.ToJson(reqClassInfoModel);
            var result = Request<resShareListMain>("/api/resources/getShareList", data, "共享资源包列表");
            if (result.Item1)
            {

                return result.Item2.data.data;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取上课资源
        /// </summary>
        public List<ResBkBeginClassResListModel> GetClassResourceDetail(string bkId)
        {
            ReqBkClassDetailModel model = new ReqBkClassDetailModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                bid = bkId,
            };

            string data = JsonHelper.ToJson(model);
            var result = Request<ResBkBeginClassMainModel>("/api/resources/beikelist", data, "备课资源详情");
            if (result.Item1)
            {
                return result.Item2.data.resource_list;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 下载备课包
        /// </summary>
        public void DownBkResource(int Id, string downUrl)
        {
            string path = SaveZip(downUrl);
            if (string.IsNullOrWhiteSpace(path))
            {
                //ShowDialogView("请选择下载路径!");
                return;
            }
            //ReqUpResourceModel model = new ReqUpResourceModel()
            //{
            //    token = HuiZeeducation.Instance.UserInfo.token,
            //    id = Id
            //};
            //string data = JsonHelper.ToJson(model);

            //var result0 = RequestDown(downUrl, data, "下载备课包");
            //if (string.IsNullOrWhiteSpace(result0.ToString()))
            //{
            //    return;

            //}

            //string parm = "?token=" + HuiZeeducation.Instance.UserInfo.token + "&id=" + Id;
            var result = RequestFile(downUrl, "下载备课包", path);
            if (result)
            {
                ShowDialogView("下载成功!");
                //return result;
            }
            else
            {
                //return "";
            }
        }

        /// <summary>
        /// 备课包一键引用
        /// </summary>
        public void BkResourcesQuote(int Id)
        {
            ReqBkQutoModel model = new ReqBkQutoModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                id = Id,
            };
            string data = JsonHelper.ToJson(model);
            var result = Request<ResDelBkModel>("/api/resources/quoteClass", data, "备课包一键引用");
            if (result.Item1)
            {
                ShowDialogView("备课包一键引用成功!");
            }
            else
            {
                ShowDialogView(result.Item2.msg);
            }
        }
    }
}
